package cn.itcast.web.servlet;

import cn.itcast.daomain.user;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/LoginUserServlet")
public class LoginUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession  session = request.getSession();
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        Boolean tui= Boolean.valueOf(request.getParameter("tuichu"));
        if (tui){

            session.removeAttribute("usersList");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else {

            UserService service=new UserServiceImpl();
            user users=service.login(username,password);
            if (users==null||users.equals("")){
                request.setAttribute("name","账号或者密码错误请重新输入");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }else {
                session.setAttribute("usersList", users);
                if (users.getPhone()!=null&&!users.getPhone().equals("")){
                    System.out.println("进入");
                    session.setAttribute("namecode","true");
                }else {
                    System.out.println("没有进入");
                    session.setAttribute("namecode","false");
                }
                response.sendRedirect(request.getContextPath() + "/index.jsp");
//                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }
        }

    }
}
